home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GDEVMEM < prev    next >
Text File  |  1991-10-25  |  28KB  |  915 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevmem.c */
  21. /* "Memory" (stored bitmap) device for Ghostscript library. */
  22. #include "memory_.h"
  23. #include "gs.h"
  24. #include "arch.h"
  25. #include "gxbitmap.h"
  26. #include "gsmatrix.h"            /* for gxdevice.h */
  27. #include "gxdevice.h"
  28. #include "gxdevmem.h"
  29.  
  30. /*
  31.    The obvious representation for a "memory" device is simply a
  32.    contiguous bitmap stored in something like the PostScript
  33.    representation, i.e., each scan line (in left-to-right order), padded
  34.    to a byte or word boundary, followed immediately by the next one.
  35.    Unfortunately, we can't use this representation, for two reasons:
  36.  
  37.     - On PCs with segmented architectures, there is no way to
  38.       obtain a contiguous block of storage larger than 64K bytes,
  39.       which isn't big enough for a full-screen bitmap, even in
  40.       monochrome.
  41.  
  42.     - The representation of strings in the Ghostscript
  43.       interpreter limits the size of a string to 64K-1 bytes,
  44.       which means we can't simply use a string for the contents
  45.       of a memory device.
  46.  
  47.    We get around the former problem by representing a memory device
  48.    as an array of strings: each string holds one scan line.
  49.    We get around the latter problem by making the client read out the
  50.    contents of a memory device bitmap in pieces.
  51. */
  52.  
  53. /* ------ Generic macros ------ */
  54.  
  55. /* Macro for declaring the essential device procedures. */
  56. #define declare_mem_map_procs(map_rgb_color, map_color_rgb)\
  57.   private dev_proc_map_rgb_color(map_rgb_color);\
  58.   private dev_proc_map_color_rgb(map_color_rgb)
  59. #define declare_mem_procs(copy_mono, copy_color, fill_rectangle)\
  60.   private dev_proc_copy_mono(copy_mono);\
  61.   private dev_proc_copy_color(copy_color);\
  62.   private dev_proc_fill_rectangle(fill_rectangle)
  63.  
  64. /* Macro for generating the procedure record in the device descriptor */
  65. #define mem_procs(map_rgb_color, map_color_rgb, copy_mono, copy_color, fill_rectangle)\
  66. {    mem_open,\
  67.     mem_get_initial_matrix,\
  68.     gx_default_sync_output,\
  69.     gx_default_output_page,\
  70.     gx_default_close_device,\
  71.     map_rgb_color,            /* differs */\
  72.     map_color_rgb,            /* differs */\
  73.     fill_rectangle,            /* differs */\
  74.     gx_default_tile_rectangle,\
  75.     copy_mono,            /* differs */\
  76.     copy_color,            /* differs */\
  77.     gx_default_draw_line,\
  78.     gx_default_fill_trapezoid,\
  79.     gx_default_tile_trapezoid\
  80. }
  81.  
  82. /* Macro for generating the device descriptor. */
  83. /* The "& 15" in max_value is bogus, to keep certain compilers */
  84. /* from complaining about a left shift by 32. */
  85. #define max_value(depth) (depth > 8 ? 255 : (1 << (depth & 15)) - 1)
  86. #define mem_device(name, depth, procs)\
  87. {    sizeof(gx_device_memory),\
  88.     &procs,            /* differs */\
  89.     name,            /* differs */\
  90.     0, 0,            /* x and y extent (filled in) */\
  91.     1, 1,            /* density (irrelevant) */\
  92.     no_margins,        /* margins */\
  93.     (depth > 1),        /* has_color */\
  94.     max_value(depth),    /* max_rgb */\
  95.     depth,            /* depth differs */\
  96.     0,            /* not open yet */\
  97.     identity_matrix_body,    /* initial matrix (filled in) */\
  98.     0,            /* raster (filled in) */\
  99.     (byte *)0,        /* base (filled in) */\
  100.     (byte **)0,        /* line_ptrs (filled in by 'open') */\
  101.     0,            /* initial bytes_le (filled in by 'open') */\
  102.     0,            /* invert (filled in for mono) */\
  103.     0, (byte *)0        /* palette (filled in for color) */\
  104. }
  105.  
  106. /* Macro for casting gx_device argument */
  107. #define mdev ((gx_device_memory *)dev)
  108.  
  109. /* Macro for rectangle arguments (x,y,w,h) */
  110. #define check_rect()\
  111.     if ( w <= 0 || h <= 0 ) return 0;\
  112.     if ( x < 0 || x > mdev->width - w || y < 0 || y > mdev->height - h )\
  113.         return -1
  114.  
  115. /*
  116.  * Macros for processing bitmaps in the largest possible chunks.
  117.  * Bits within a byte are always stored big-endian;
  118.  * bytes are normally stored in the natural order for the platform,
  119.  * but the client can force them into big-endian order (which is required
  120.  * for the source of copy_mono) by calling gdev_mem_ensure_byte_order.
  121.  * (gdev_mem_copy_scan_lines swaps bytes if necessary.)
  122.  * Note that we use type uint for register variables holding a chunk:
  123.  * for this reason, the chunk size cannot be larger than uint.
  124.  */
  125. /******
  126.  ****** Note: we assume that ints are 2 or 4 bytes in size!
  127.  ****** (This is required by the clog2_bits macro.)
  128.  ******/
  129. /* Generic macros for chunk accessing. */
  130. #define cbytes(ct) ((int)sizeof(ct))    /* sizeof may be unsigned */
  131. #  define chunk_bytes cbytes(chunk)
  132. #define clog2_bytes(ct) ((int)sizeof(ct)>>1)    /* works for 1,2,4! */
  133. #  define chunk_log2_bytes clog2_bytes(chunk)
  134. #define cbits(ct) ((int)sizeof(ct)*8)    /* sizeof may be unsigned */
  135. #  define chunk_bits cbits(chunk)
  136. #define clog2_bits(ct) (clog2_bytes(ct)+3)
  137. #  define chunk_log2_bits clog2_bits(chunk)
  138. #define cbit_mask(ct) (cbits(ct)-1)
  139. #  define chunk_bit_mask cbit_mask(chunk)
  140. #define chi_bit(ct) ((ct)1<<(cbits(ct)-1))
  141. #  define chunk_hi_bit chi_bit(chunk)
  142. #define cmask(ct) ((ct)~0)
  143. #  define chunk_all_bits cmask(chunk)
  144. #define chi_bits(ct,n) (cmask(ct)-(cmask(ct)>>(n)))
  145. #  define chunk_hi_bits(n) chi_bits(chunk,n)
  146.  
  147. /* Macros for scan line access. */
  148. /* x_to_byte is different for each number of bits per pixel. */
  149. /* Note that these macros depend on the definition of chunk: */
  150. /* each procedure that uses the scanning macros should #define */
  151. /* (not typedef) chunk as either uint or byte. */
  152. #define declare_scan_line(line,ptr)\
  153.     byte **line; register chunk *ptr
  154. #define declare_scan_ptr(line,ptr,offset)\
  155.     byte **line; chunk *ptr; int offset
  156. #define setup_scan_ptr(line,ptr,offset)\
  157.     ptr = (chunk *)((*line) + (offset))
  158. #define setup_scan(line,ptr,offset)\
  159.     line = mdev->line_ptrs + (y);\
  160.     setup_scan_ptr(line,ptr,offset)
  161. #define next_scan_line(line,ptr,offset)\
  162.     ++line; setup_scan_ptr(line,ptr,offset)
  163. #define setup_rect(line,ptr,offset)\
  164.     offset = x_to_byte(x) & -chunk_bytes;\
  165.     setup_scan(line,ptr,offset)
  166.  
  167. /* ------ Generic code ------ */
  168.  
  169. /* Compute the size of the bitmap storage, */
  170. /* including the space for the scan line index. */
  171. /* Note that scan lines are padded to a multiple of 4 bytes. */
  172. ulong
  173. gdev_mem_bitmap_size(gx_device_memory *dev)
  174. {    unsigned raster =
  175.         ((mdev->width * mdev->bits_per_color_pixel + 31) >> 5) << 2;
  176.     mdev->raster = raster;
  177.     return (ulong)mdev->height * (raster + sizeof(byte *));
  178. }
  179.  
  180. /* 'Open' the memory device by creating the index table if needed. */
  181. private int
  182. mem_open(gx_device *dev)
  183. {
  184. #ifdef __MSDOS__            /* ****** NOTA BENE ****** */
  185.     int z = 0;
  186.     byte huge *scan_line = mdev->base + z;    /* force normalization */
  187. #else                    /* ****** ****** */
  188.     byte *scan_line = mdev->base;
  189. #endif                    /* ****** ****** */
  190.     byte **pptr = (byte **)(scan_line + (ulong)mdev->height * mdev->raster);
  191.     byte **pend = pptr + mdev->height;
  192.     mdev->line_ptrs = pptr;
  193.     while ( pptr != pend )
  194.        {    *pptr++ = (byte *)scan_line;
  195.         scan_line += mdev->raster;
  196.        }
  197.     mdev->bytes_le =
  198. #if arch_is_big_endian
  199.         0
  200. #else
  201.         !mdev->has_color
  202. #endif
  203.         ;
  204.     return 0;
  205. }
  206.  
  207. /* Return the initial transformation matrix */
  208. void
  209. mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  210. {    *pmat = mdev->initial_matrix;
  211. }
  212.  
  213. /* Test whether a device is a memory device */
  214. int
  215. gs_device_is_memory(gx_device *dev)
  216. {    /* We can't just compare the procs, or even an individual proc, */
  217.     /* because we might be tracing.  Compare the device name, */
  218.     /* and hope for the best. */
  219.     char *name = dev->dname;
  220.     int i;
  221.     for ( i = 0; i < 6; i++ )
  222.       if ( name[i] != "image("[i] ) return 0;
  223.     return 1;
  224. }
  225.  
  226. /* Compute the number of data bytes per scan line. */
  227. /* Note that this does not include the padding. */
  228. int
  229. gdev_mem_bytes_per_scan_line(gx_device *dev)
  230. {    return (dev->width * dev->bits_per_color_pixel + 7) >> 3;
  231. }
  232.  
  233. /* Ensure that the data bytes are in big-endian order. */
  234. /* This is never called on big-endian platforms; */
  235. /* on little-endian platforms, the chunk size is ushort, */
  236. /* regardless of the size of an int. */
  237. private void
  238. mem_swap_bytes(gx_device_memory *dev, byte *row)
  239. {    register ushort *ptr = (ushort *)row;
  240.     register ushort w;
  241.     register int x;
  242.     for ( x = dev->raster >> 1; --x >= 0; ptr++ )
  243.        {    w = *ptr;
  244.         *ptr = (w >> 8) + (w << 8);
  245.        }
  246. }
  247. void
  248. gdev_mem_ensure_byte_order(gx_device_memory *dev)
  249. {
  250. #if !arch_is_big_endian
  251.     byte **lptr;
  252.     int y;
  253.     if ( !dev->bytes_le ) return;    /* already in order */
  254.     lptr = dev->line_ptrs;
  255.     for ( y = dev->height; --y >= 0; )
  256.         mem_swap_bytes(dev, *lptr++);
  257.     dev->bytes_le = 0;
  258. #endif
  259. }
  260.  
  261. /* Copy one or more scan lines to a client. */
  262. #undef chunk
  263. #define chunk byte
  264. int
  265. gdev_mem_copy_scan_lines(gx_device_memory *dev, int start_y, byte *str,
  266.   uint size)
  267. {    declare_scan_ptr(src_line, src, offset);
  268.     uint bytes_per_line = gdev_mem_bytes_per_scan_line((gx_device *)dev);
  269.     byte *dest = str;
  270.     int y = start_y;
  271.     uint count = min(size / bytes_per_line, dev->height - y);
  272.     int swap = mdev->bytes_le;
  273.     if ( !mdev->raster )        /* compute it now */
  274.         (void)gdev_mem_bitmap_size(mdev);
  275.     setup_scan(src_line, src, 0);
  276.     while ( count-- != 0 )
  277.        {    if ( swap ) mem_swap_bytes(mdev, src);
  278.         memcpy(dest, src, bytes_per_line);
  279.         if ( swap ) mem_swap_bytes(mdev, src);    /* swap back */
  280.         next_scan_line(src_line, src, 0);
  281.         dest += bytes_per_line;
  282.         y++;
  283.        }
  284.     return y - start_y;
  285. }
  286.  
  287. /* ------ Monochrome ------ */
  288.  
  289. /* Procedures */
  290. declare_mem_procs(mem_mono_copy_mono, mem_mono_copy_color, mem_mono_fill_rectangle);
  291.  
  292. /* The device descriptor. */
  293. private gx_device_procs mem_mono_procs =
  294.   mem_procs(gx_default_map_rgb_color, gx_default_map_color_rgb,
  295.     mem_mono_copy_mono, mem_mono_copy_color, mem_mono_fill_rectangle);
  296.  
  297. /* The instance is public. */
  298. gx_device_memory mem_mono_device =
  299.   mem_device("image(mono)", 1, mem_mono_procs);
  300.  
  301. /* Convert x coordinate to byte offset in scan line. */
  302. #define x_to_byte(x) ((x) >> 3)
  303.  
  304. /* Fill a rectangle with a color. */
  305. #undef chunk
  306. #if arch_is_big_endian
  307. #  define chunk uint
  308. #else
  309. #  define chunk ushort
  310. #endif
  311. private int
  312. mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  313.   gx_color_index color)
  314. {    uint bit;
  315.     chunk right_mask;
  316.     byte fill;
  317.     declare_scan_ptr(dest_line, dest, offset);
  318.     check_rect();
  319.     setup_rect(dest_line, dest, offset);
  320. #define write_loop(stat)\
  321.  { int line_count = h;\
  322.    declare_scan_line(ptr_line, ptr);\
  323.    ptr_line = dest_line;\
  324.    setup_scan_ptr(ptr_line, ptr, offset);\
  325.    do { stat; next_scan_line(ptr_line, ptr, offset); }\
  326.    while ( --line_count );\
  327.  }
  328. #define write_partial(msk)\
  329.    if ( fill ) write_loop(*ptr |= msk)\
  330.    else write_loop(*ptr &= ~msk)
  331.     switch ( color )
  332.        {
  333.     case 0: fill = mdev->invert; break;
  334.     case 1: fill = ~mdev->invert; break;
  335.     case gx_no_color_index: return 0;        /* transparent */
  336.     default: return -1;        /* invalid */
  337.        }
  338.     bit = x & chunk_bit_mask;
  339.     if ( bit + w <= chunk_bits )
  340.        {    /* Only one word */
  341.         right_mask = chunk_hi_bits(w) >> bit;
  342.        }
  343.     else
  344.        {    int byte_count;
  345.         if ( bit )
  346.            {    chunk mask = chunk_all_bits >> bit;
  347.             write_partial(mask);
  348.             offset += chunk_bytes;
  349.             w += bit - chunk_bits;
  350.            }
  351.         right_mask = chunk_hi_bits(w & chunk_bit_mask);
  352.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  353.            {    write_loop(memset(ptr, fill, byte_count));
  354.             offset += byte_count;
  355.            }
  356.        }
  357.     if ( right_mask )
  358.         write_partial(right_mask);
  359.     return 0;
  360. }
  361.  
  362. /* Copy a monochrome bitmap. */
  363.  
  364. /* Fetch a chunk from the source. */
  365. /* Note that the source data are always stored big-endian. */
  366. #undef chunk
  367. #if arch_is_big_endian
  368. #  define chunk uint
  369. #  define cfetch(cptr) (*(chunk *)(cptr))
  370. #else
  371. #  define chunk ushort
  372. #  define cfetch(cptr)\
  373.     ((chunk)(*(chunk *)(cptr) << 8) + (*(chunk *)(cptr) >> 8))
  374. #endif
  375. /* Fetch a chunk that straddles a chunk boundary. */
  376. #define cfetch2(cptr, cskew, skew)\
  377.   ((cfetch(cptr) << cskew) + (cfetch((chunk *)cptr + 1) >> skew))
  378.  
  379. /* Define the 9 possible cases of zero and one values. */
  380. /* Some of them are trivial. */
  381. typedef enum {
  382.     copyNN = 0, copyN0 = 1, copyN1 = 2,
  383.     copy0N = 3, copy00 = 4, copy01 = 5,
  384.     copy1N = 6, copy10 = 7, copy11 = 8
  385. } copy_case;
  386. typedef enum {
  387.     copy_and = -1, copy_store = 0, copy_or = 1
  388. } copy_function;
  389. private int
  390. mem_mono_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  391.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  392. {    byte *line;            /* actually chunk * */
  393.     int sleft, dleft, wleft;
  394.     register int skew;
  395.     uint mask;
  396.     register uint invert;
  397.     copy_case ccase;
  398.     copy_function function;
  399.     declare_scan_ptr(dest_line, dest, offset);
  400. #if gx_no_color_value != -1        /* hokey! */
  401.     if ( zero == gx_no_color_index ) zero = -1;
  402.     if ( one == gx_no_color_index ) one = -1;
  403. #endif
  404. #define izero (int)zero
  405. #define ione (int)one
  406.     ccase = (copy_case)(izero + izero + izero + ione + 4);
  407. #undef izero
  408. #undef ione
  409.     if ( mdev->invert )
  410.        {    static copy_case invert_ccase[9] =
  411.            {    copyNN, copyN1, copyN0,
  412.             copy1N, copy11, copy10,
  413.             copy0N, copy01, copy00
  414.            };
  415.         ccase = invert_ccase[(int)ccase];
  416.        }
  417.     switch ( (int)ccase )
  418.        {
  419.     case (int)copyNN:        /* can't happen */
  420.         return -1;
  421.     case (int)copy00:        /* just fill */
  422.     case (int)copy11:
  423.         return mem_mono_fill_rectangle(dev, x, y, w, h, zero);
  424.     case (int)copy01:
  425.         invert = 0, function = copy_store; break;
  426.     case (int)copy0N:
  427.         invert = 0, function = copy_and; break;
  428.     case (int)copyN1:
  429.         invert = 0, function = copy_or; break;
  430.     case (int)copy10:
  431.         invert = -1, function = copy_store; break;
  432.     case (int)copyN0:
  433.         invert = -1, function = copy_and; break;
  434.     case (int)copy1N:
  435.         invert = -1, function = copy_or; break;
  436.        }
  437.     check_rect();
  438.     setup_rect(dest_line, dest, offset);
  439.     line = base + (sourcex & ~chunk_bit_mask);
  440.     sleft = chunk_bits - (sourcex & chunk_bit_mask);
  441.     dleft = chunk_bits - (x & chunk_bit_mask);
  442.     skew = sleft - dleft;
  443.     mask = chunk_all_bits >> (chunk_bits - dleft);
  444. /* Macros for writing partial chunks. */
  445. /* The destination pointer is always named optr. */
  446. #define write_or_masked(bits, mask)\
  447.   *optr |= (((bits) ^ invert) & mask)
  448. #define write_store_masked(bits, mask)\
  449.   *optr = ((*optr & ~mask) | (((bits) ^ invert) & mask))
  450. #define write_and_masked(bits, mask)\
  451.   *optr &= (((bits) ^ invert) | ~mask)
  452. #define write_chunk_masked(bits, mask)\
  453.   switch ( function ) {\
  454.     case copy_or: write_or_masked(bits, mask); break;\
  455.     case copy_store: write_store_masked(bits, mask); break;\
  456.     default: write_and_masked(bits, mask);\
  457.   }
  458. #define write_or(bits)  *optr |= (bits) ^ invert
  459. #define write_store(bits) *optr = (bits) ^ invert
  460. #define write_and(bits) *optr &= (bits) ^ invert
  461.     if ( (wleft = w - dleft) <= 0 )
  462.        {    /* The entire operation fits in one chunk. */
  463.         register byte *bptr = line;    /* actually chunk * */
  464.         register chunk *optr = dest;
  465.         mask -= mask >> w;
  466. #define write1_loop(src)\
  467.   for ( ; ; )\
  468.    { write_chunk_masked(src, mask);\
  469.      if ( --h == 0 ) break;\
  470.      next_scan_line(dest_line, optr, offset);\
  471.      bptr += raster;\
  472.    }
  473.         if ( skew >= 0 )    /* single -> single, right/no shift */
  474.            {    write1_loop(cfetch(bptr) >> skew);
  475.            }
  476.         else if ( w <= sleft )    /* single -> single, left shift */
  477.            {    skew = -skew;
  478.             write1_loop(cfetch(bptr) << skew);
  479.            }
  480.         else            /* single -> double */
  481.            {    int cskew = -skew;
  482.             skew += chunk_bits;
  483.             write1_loop(cfetch2(bptr, cskew, skew));
  484.            }
  485.        }
  486.     else
  487.        {    /* More than one chunk is involved. */
  488.         uint rmask = chunk_hi_bits(wleft & chunk_bit_mask);
  489.         if ( sleft == dleft )        /* optimize the aligned case */
  490.            {    for ( ; ; )
  491.                {    register chunk *bptr = (chunk *)line;
  492.                 register chunk *optr = dest;
  493.                 int count = wleft;
  494. #define write_aligned(wr_op, wr_op_masked)\
  495.   /* Do first partial chunk. */\
  496.   wr_op_masked(cfetch(bptr), mask);\
  497.   /* Do full chunks. */\
  498.   while ( (count -= chunk_bits) >= 0 )\
  499.     { ++bptr; ++optr; wr_op(cfetch(bptr)); }\
  500.   /* Do last chunk */\
  501.   if ( count > -chunk_bits )\
  502.     { ++bptr; ++optr; wr_op_masked(cfetch(bptr), rmask); }
  503.                 switch ( function )
  504.                   {
  505.                   case copy_or:
  506.                     write_aligned(write_or, write_or_masked);
  507.                     break;
  508.                   case copy_store:
  509.                     write_aligned(write_store, write_store_masked);
  510.                     break;
  511.                   default: /* copy_and */
  512.                     write_aligned(write_and, write_and_masked);
  513.                   }
  514. #undef write_aligned
  515.                 if ( --h == 0 ) break;
  516.                 next_scan_line(dest_line, dest, offset);
  517.                 line += raster;
  518.                }
  519.            }
  520.         else
  521.            {    int cskew = -skew & chunk_bit_mask;
  522.             skew &= chunk_bit_mask;
  523.             for ( ; ; )
  524.                {    register chunk *bptr = (chunk *)line;
  525.                 register uint bits;
  526.                 register chunk *optr = dest;
  527.                 int count = wleft;
  528.                 /* Do the first partial chunk */
  529.                 if ( sleft >= dleft )
  530.                    {    bits = cfetch(bptr) >> skew;
  531.                    }
  532.                 else /* ( sleft < dleft ) */
  533.                    {    bits = cfetch(bptr) << cskew;
  534.                     ++bptr;
  535.                     if ( w > sleft )
  536.                         bits += cfetch(bptr) >> skew;
  537.                    }
  538. #define write_unaligned(wr_op, wr_op_masked)\
  539.   wr_op_masked(bits, mask);\
  540.   /* Do full chunks. */\
  541.   while ( count >= chunk_bits )\
  542.     { bits = cfetch2(bptr, cskew, skew);\
  543.       ++bptr; ++optr; wr_op(bits); count -= chunk_bits;\
  544.     }\
  545.   /* Do last chunk */\
  546.   if ( count > 0 )\
  547.     { bits = cfetch(bptr) << cskew;\
  548.       ++bptr; if ( count > skew ) bits += cfetch(bptr) >> skew;\
  549.       ++optr; wr_op_masked(bits, rmask);\
  550.     }
  551.                 switch ( function )
  552.                   {
  553.                   case copy_or:
  554.                     write_unaligned(write_or, write_or_masked);
  555.                     break;
  556.                   case copy_store:
  557.                     write_unaligned(write_store, write_store_masked);
  558.                     break;
  559.                   default: /* copy_and */
  560.                     write_unaligned(write_and, write_and_masked);
  561.                   }
  562. #undef write_unaligned
  563.                 if ( --h == 0 ) break;
  564.                 next_scan_line(dest_line, dest, offset);
  565.                 line += raster;
  566.                }
  567.            }
  568.        }
  569.     return 0;
  570. }
  571.  
  572. /* Copy a "color" bitmap.  Since "color" is the same as monochrome, */
  573. /* this just reduces to copying a monochrome bitmap. */
  574. private int
  575. mem_mono_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  576.   int x, int y, int w, int h)
  577. {    return mem_mono_copy_mono(dev, base, sourcex, raster, x, y, w, h,
  578.       (gx_color_index)0, (gx_color_index)1);
  579. }
  580.  
  581. /* ------ Color (mapped or true) ------ */
  582.  
  583. /* Copy a rectangle of bytes from a source to a destination. */
  584. #undef chunk
  585. #define chunk byte
  586. private int
  587. copy_byte_rect(gx_device *dev, byte *source, int sraster,
  588.   int offset, int y, int w, int h)
  589. {    declare_scan_line(dest_line, dest);
  590.     setup_scan(dest_line, dest, offset);
  591.     while ( h-- > 0 )
  592.        {    memcpy(dest, source, w);
  593.         source += sraster;
  594.         next_scan_line(dest_line, dest, offset);
  595.        }
  596.     return 0;
  597. }
  598.  
  599. /* ------ Mapped 8-bit color ------ */
  600.  
  601. /* Procedures */
  602. declare_mem_map_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb);
  603. declare_mem_procs(mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  604.  
  605. /* The device descriptor. */
  606. private gx_device_procs mem_mapped8_procs =
  607.   mem_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  608.     mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  609.  
  610. /* The instance is public. */
  611. gx_device_memory mem_mapped8_color_device =
  612.   mem_device("image(8)", 8, mem_mapped8_procs);
  613.  
  614. /* Convert x coordinate to byte offset in scan line. */
  615. #undef x_to_byte
  616. #define x_to_byte(x) (x)
  617.  
  618. /* Map a r-g-b color to a color index. */
  619. /* This requires searching the palette. */
  620. private gx_color_index
  621. mem_mapped_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  622. {    register byte *pptr = mdev->palette;
  623.     int cnt = mdev->palette_size;
  624.     byte *which;
  625.     int best = 256*3;
  626.     while ( cnt-- > 0 )
  627.        {    register int diff = *pptr - r;
  628.         if ( diff < 0 ) diff = -diff;
  629.         if ( diff < best )    /* quick rejection */
  630.            {    int dg = pptr[1] - g;
  631.             if ( dg < 0 ) dg = -dg;
  632.             if ( (diff += dg) < best )    /* quick rejection */
  633.                {    int db = pptr[2] - b;
  634.                 if ( db < 0 ) db = -db;
  635.                 if ( (diff += db) < best )
  636.                     which = pptr, best = diff;
  637.                }
  638.            }
  639.         pptr += 3;
  640.        }
  641.     return (gx_color_index)((which - mdev->palette) / 3);
  642. }
  643.  
  644. /* Map a color index to a r-g-b color. */
  645. private int
  646. mem_mapped_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  647. {    byte *pptr = mdev->palette + (int)color * 3;
  648.     prgb[0] = pptr[0];
  649.     prgb[1] = pptr[1];
  650.     prgb[2] = pptr[2];
  651.     return 0;
  652. }
  653.  
  654. /* Fill a rectangle with a color. */
  655. private int
  656. mem_mapped8_fill_rectangle(gx_device *dev,
  657.   int x, int y, int w, int h, gx_color_index color)
  658. {    declare_scan_ptr(dest_line, dest, offset);
  659.     check_rect();
  660.     setup_rect(dest_line, dest, offset);
  661.     while ( h-- > 0 )
  662.        {    memset(dest, (byte)color, w);
  663.         next_scan_line(dest_line, dest, offset);
  664.        }
  665.     return 0;
  666. }
  667.  
  668. /* Copy a monochrome bitmap. */
  669. private int
  670. mem_mapped8_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  671.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  672. {    byte *line;
  673.     int first_bit;
  674.     declare_scan_ptr(dest_line, dest, offset);
  675.     check_rect();
  676.     setup_rect(dest_line, dest, offset);
  677.     line = base + (sourcex >> 3);
  678.     first_bit = 0x80 >> (sourcex & 7);
  679.     while ( h-- > 0 )
  680.        {    register byte *pptr = dest;
  681.         byte *sptr = line;
  682.         register int sbyte = *sptr++;
  683.         register int bit = first_bit;
  684.         int count = w;
  685.         do
  686.            {    if ( sbyte & bit )
  687.                {    if ( (int)one != (int)gx_no_color_index )
  688.                   *pptr = (byte)one;
  689.                }
  690.             else
  691.                {    if ( (int)zero != (int)gx_no_color_index )
  692.                   *pptr = (byte)zero;
  693.                }
  694.             if ( (bit >>= 1) == 0 )
  695.                 bit = 0x80, sbyte = *sptr++;
  696.             pptr++;
  697.            }
  698.         while ( --count > 0 );
  699.         line += raster;
  700.         next_scan_line(dest_line, dest, offset);
  701.        }
  702.     return 0;
  703. }
  704.  
  705. /* Copy a color bitmap. */
  706. private int
  707. mem_mapped8_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  708.   int x, int y, int w, int h)
  709. {    check_rect();
  710.     return copy_byte_rect(dev, base + x_to_byte(sourcex), raster,
  711.         x_to_byte(x), y, x_to_byte(w), h);
  712. }
  713.  
  714. /* ------ True (24- or 32-bit) color ------ */
  715.  
  716. /* Procedures */
  717. declare_mem_map_procs(mem_true_map_rgb_color, mem_true_map_color_rgb);
  718.  
  719. /* The device descriptor. */
  720. #define mem_true_procs(copy_mono, copy_color, fill_rectangle)\
  721.   mem_procs(mem_true_map_rgb_color, mem_true_map_color_rgb,\
  722.     copy_mono, copy_color, fill_rectangle)
  723.  
  724. /* We want the bytes of a color always to be in the order -,r,g,b, */
  725. /* but we want to manipulate colors as longs.  This requires careful */
  726. /* handling to be byte-order independent. */
  727. #define color_byte(cx,i) (((byte *)&(cx))[i])
  728.  
  729. /* Map a r-g-b color to a color index. */
  730. private gx_color_index
  731. mem_true_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  732. {    gx_color_index color = 0;
  733.     color_byte(color, 1) = r;
  734.     color_byte(color, 2) = g;
  735.     color_byte(color, 3) = b;
  736.     return color;
  737. }
  738.  
  739. /* Map a color index to a r-g-b color. */
  740. private int
  741. mem_true_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  742. {    prgb[0] = color_byte(color, 1);
  743.     prgb[1] = color_byte(color, 2);
  744.     prgb[2] = color_byte(color, 3);
  745.     return 0;
  746. }
  747.  
  748. /* ------ 24-bit color ------ */
  749. /* 24-bit takes less space than 32-bit, but is slower. */
  750.  
  751. /* Procedures */
  752. declare_mem_procs(mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
  753.  
  754. /* The device descriptor. */
  755. private gx_device_procs mem_true24_procs =
  756.   mem_true_procs(mem_true24_copy_mono, mem_true24_copy_color,
  757.     mem_true24_fill_rectangle);
  758. gx_device_memory mem_true24_color_device =
  759.   mem_device("image(24)", 24, mem_true24_procs);
  760.  
  761. /* Convert x coordinate to byte offset in scan line. */
  762. #undef x_to_byte
  763. #define x_to_byte(x) ((x) * 3)
  764.  
  765. /* Unpack a color into its bytes. */
  766. #define declare_unpack_color(r, g, b, color)\
  767.     byte r = color_byte(color, 1);\
  768.     byte g = color_byte(color, 2);\
  769.     byte b = color_byte(color, 3)
  770. #define put3(ptr, r, g, b)\
  771.     ptr[0] = r, ptr[1] = g, ptr[2] = b
  772.  
  773. /* Fill a rectangle with a color. */
  774. private int
  775. mem_true24_fill_rectangle(gx_device *dev,
  776.   int x, int y, int w, int h, gx_color_index color)
  777. {    declare_unpack_color(r, g, b, color);
  778.     declare_scan_ptr(dest_line, dest, offset);
  779.     check_rect();
  780.     setup_rect(dest_line, dest, offset);
  781.     while ( h-- > 0 )
  782.        {    register int cnt = w;
  783.         register byte *pptr = dest;
  784.         do { put3(pptr, r, g, b); pptr += 3; } while ( --cnt > 0 );
  785.         next_scan_line(dest_line, dest, offset);
  786.        }
  787.     return 0;
  788. }
  789.  
  790. /* Copy a monochrome bitmap. */
  791. private int
  792. mem_true24_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  793.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  794. {    byte *line;
  795.     int first_bit;
  796.     declare_unpack_color(r0, g0, b0, zero);
  797.     declare_unpack_color(r1, g1, b1, one);
  798.     declare_scan_ptr(dest_line, dest, offset);
  799.     check_rect();
  800.     setup_rect(dest_line, dest, offset);
  801.     line = base + (sourcex >> 3);
  802.     first_bit = 0x80 >> (sourcex & 7);
  803.     while ( h-- > 0 )
  804.        {    register byte *pptr = dest;
  805.         byte *sptr = line;
  806.         register int sbyte = *sptr++;
  807.         register int bit = first_bit;
  808.         int count = w;
  809.         do
  810.            {    if ( sbyte & bit )
  811.                {    if ( one != gx_no_color_index )
  812.                   put3(pptr, r1, g1, b1);
  813.                }
  814.             else
  815.                {    if ( zero != gx_no_color_index )
  816.                   put3(pptr, r0, g0, b0);
  817.                }
  818.             pptr += 3;
  819.             if ( (bit >>= 1) == 0 )
  820.                 bit = 0x80, sbyte = *sptr++;
  821.            }
  822.         while ( --count > 0 );
  823.         line += raster;
  824.         next_scan_line(dest_line, dest, offset);
  825.        }
  826.     return 0;
  827. }
  828.  
  829. /* Copy a color bitmap. */
  830. private int
  831. mem_true24_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  832.   int x, int y, int w, int h)
  833. {    check_rect();
  834.     return copy_byte_rect(dev, base + x_to_byte(sourcex), raster,
  835.         x_to_byte(x), y, x_to_byte(w), h);
  836. }
  837.  
  838. /* ------ 32-bit color ------ */
  839.  
  840. /* Procedures */
  841. declare_mem_procs(mem_true32_copy_mono, mem_true32_copy_color, mem_true32_fill_rectangle);
  842.  
  843. /* The device descriptor. */
  844. private gx_device_procs mem_true32_procs =
  845.   mem_true_procs(mem_true32_copy_mono, mem_true32_copy_color,
  846.     mem_true32_fill_rectangle);
  847. gx_device_memory mem_true32_color_device =
  848.   mem_device("image(32)", 32, mem_true32_procs);
  849.  
  850. /* Convert x coordinate to byte offset in scan line. */
  851. #undef x_to_byte
  852. #define x_to_byte(x) ((x) << 2)
  853.  
  854. /* Fill a rectangle with a color. */
  855. private int
  856. mem_true32_fill_rectangle(gx_device *dev,
  857.   int x, int y, int w, int h, gx_color_index color)
  858. {    declare_scan_ptr(dest_line, dest, offset);
  859.     check_rect();
  860.     setup_rect(dest_line, dest, offset);
  861.     while ( h-- > 0 )
  862.        {    gx_color_index *pptr = (gx_color_index *)dest;
  863.         int cnt = w;
  864.         do { *pptr++ = color; } while ( --cnt > 0 );
  865.         next_scan_line(dest_line, dest, offset);
  866.        }
  867.     return 0;
  868. }
  869.  
  870. /* Copy a monochrome bitmap. */
  871. private int
  872. mem_true32_copy_mono(gx_device *dev, byte *base, int sourcex, int raster,
  873.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  874. {    byte *line;
  875.     int first_bit;
  876.     declare_scan_ptr(dest_line, dest, offset);
  877.     check_rect();
  878.     setup_rect(dest_line, dest, offset);
  879.     line = base + (sourcex >> 3);
  880.     first_bit = 0x80 >> (sourcex & 7);
  881.     while ( h-- > 0 )
  882.        {    register gx_color_index *pptr = (gx_color_index *)dest;
  883.         byte *sptr = line;
  884.         register int sbyte = *sptr++;
  885.         register int bit = first_bit;
  886.         int count = w;
  887.         do
  888.            {    if ( sbyte & bit )
  889.                {    if ( one != gx_no_color_index )
  890.                   *pptr = one;
  891.                }
  892.             else
  893.                {    if ( zero != gx_no_color_index )
  894.                   *pptr = zero;
  895.                }
  896.             if ( (bit >>= 1) == 0 )
  897.                 bit = 0x80, sbyte = *sptr++;
  898.             pptr++;
  899.            }
  900.         while ( --count > 0 );
  901.         line += raster;
  902.         next_scan_line(dest_line, dest, offset);
  903.        }
  904.     return 0;
  905. }
  906.  
  907. /* Copy a color bitmap. */
  908. private int
  909. mem_true32_copy_color(gx_device *dev, byte *base, int sourcex, int raster,
  910.   int x, int y, int w, int h)
  911. {    check_rect();
  912.     return copy_byte_rect(dev, base + x_to_byte(sourcex), raster,
  913.         x_to_byte(x), y, x_to_byte(w), h);
  914. }
  915.